from django.shortcuts import render
from rest_framework import status

from rest_framework.views import APIView
from rest_framework.response import Response
from personal_app.models import UserCourse,UserSection
from course_app.models import Section
from personal_app.serializer import UserCourseSerializer,UserCourseListSerializer,AddSectionSerializer,UserSectionSerializer
from user_app.utils import verify_token
from user_app.models import User

# Create your views here.
class UserCourseView(APIView):
    def get(self,request,format=None):
        #获取用户唯一标识
        data = verify_token(request.META.get('HTTP_AUTHORIZATION'))
        if data:
            try:
                #查询用户信息
                user = User.objects.filter(phone=data.get('phone')).first()
            except User.DoesNotExist:
                return Response({
                    'code':status.HTTP_404_NOT_FOUND,
                    'msg':'用户信息有误'
                })
            #根据用户查询课程信息
            user_course = UserCourse.objects.filter(user=user)
            ser_data = UserCourseSerializer(user_course,many=True)
            #响应数据
            print(ser_data.data)
            return Response({
                'code':status.HTTP_200_OK,
                'msg':'获取成功',
                'data':ser_data.data
            })
    
class UserCourseListView(APIView):
    def get(self,request,format=None):
        #获取用户唯一标识
        data = verify_token(request.META.get('HTTP_AUTHORIZATION'))
        if data:
            try:
                #查询用户信息
                user = User.objects.filter(phone=data.get('phone')).first()
            except User.DoesNotExist:
                return Response({
                    'code':status.HTTP_404_NOT_FOUND,
                    'msg':'用户信息有误'
                })
            #根据用户查询课程信息
            user_course = UserCourse.objects.filter(user=user)
            ser_data = UserCourseSerializer(user_course,many=True)
            #响应数据
            print(ser_data.data)
            return Response({
                'code':status.HTTP_200_OK,
                'msg':'获取成功',
                'data':ser_data.data
            })
    
class MySectionView(APIView):
    def get(self,request,format=None):
        data = verify_token(request.META.get('HTTP_AUTHORIZATION'))
        if data:
            try:
                user = User.objects.filter(phone=data.get('phone')).first()
            except User.DoesNotExist:
                return Response({
                    'code':status.HTTP_404_NOT_FOUND,
                    'msg':'用户信息有误'
                })
            my_section = UserSection.objects.filter(user=user)
            ser_data = UserSectionSerializer(my_section,many=True)
            return Response({
                'code':status.HTTP_200_OK,
                'msg':'获取成功',
                'data':ser_data.data
            })
        
class AddSectionView(APIView):
    def post(self,request,format=None):
        serializer = AddSectionSerializer(data = request.data)
        data = verify_token(request.META.get('HTTP_AUTHORIZATION'))
        if serializer.is_valid():
            userSection = UserSection.objects.filter(section=serializer.data.get('section')).first()
            if userSection:
                return Response({
                                'code':status.HTTP_200_OK,
                                'msg':'该章节已收藏'
                            }) 
            if data:
                try:
                    user = User.objects.filter(phone=data.get('phone')).first()
                    section = Section.objects.get(id=serializer.data.get('section'))
                    UserSection.objects.create(user=user,section=section)
                    return Response({
                            'code':status.HTTP_200_OK,
                            'msg':'收藏成功'
                        }) 
                except User.DoesNotExist:
                    return Response({
                        'code':status.HTTP_404_NOT_FOUND,
                        'msg':'用户信息有误'
                    })   
        else:
            return Response({
                    'code':status.HTTP_404_NOT_FOUND,
                    'msg':'用户信息有误'
                })

